package com.blog.controller.admin;



import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.blog.entity.Blog;
import com.blog.entity.BlogType;
import com.blog.service.BlogService;
import com.blog.service.BlogTypeService;



/**
 * 后台博客管理
 * @author smfx1314
 *
 */
@Controller
@RequestMapping(value="admin")
public class BlogAdminController {
	//注入BlogService
	@Autowired
	private BlogService blogService;
	//注入blogTypeService
	@Autowired
	private BlogTypeService blogTypeService;
	/**
	 * 跳转到写博客的页面
	 */
	@RequestMapping(value="/writeBlog.do")
	public String writeBlog(Model model) {
		List<BlogType> blogTypes = blogTypeService.blogTypeList();
		model.addAttribute("blogTypes", blogTypes);
		return "writeBlog";
	}
	/**
	 * 添加博客
	 * @param blog
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/save.do",method=RequestMethod.POST)
	public String save(Blog blog) throws Exception {
		blogService.addBlog(blog);
		return "writeBlog";
	}
	/**
	 * 博客信息管理
	 */
	@RequestMapping(value="/blogInfoManager.do",method=RequestMethod.GET)
	public String blogInfoManager(HttpServletRequest request) {
		List<Blog> list=blogService.findAllBlog();
		request.setAttribute("list", list);
		return "blogInfoManager";
	}
	/**
	 * 后台删除博客
	 */
	@RequestMapping(value="/blogDelete.do",method=RequestMethod.GET)
	public String blogDelete(Blog blog) {
		blogService.deleteBlogById(blog.getId());
		return "redirect:/admin/blogInfoManager.do";
	}
	/**
	 * 后台修改博客
	 * 首先跳转到修改页面
	 */
	@RequestMapping(value="/blogupdatePage.do",method=RequestMethod.GET)
	public String blogUpdatePage(Blog blog,HttpServletRequest request) {
		Blog b=blogService.findBlogById(blog.getId());
		request.setAttribute("b", b);
		List<BlogType> blogTypeList = blogTypeService.blogTypeList();
		System.out.println(blogTypeList);
		request.setAttribute("blogTypeList", blogTypeList);
		
		return "blogUpdatePage";
	}
	/**
	 * 保存修改博客
	 */
	@RequestMapping(value="/update.do",method=RequestMethod.POST)
	public String blogUpdate(Blog blog) {
		blogService.blogUpdate(blog);
		return "redirect:/admin/blogInfoManager.do";
	}
	/**
	 * 博客类别查询
	 */
	@RequestMapping(value="/blogTypeList.do",method=RequestMethod.GET)
	public String blogType(Model model){
		List<BlogType> blogTypeList = blogTypeService.blogTypeList();
		System.out.println(blogTypeList);
		model.addAttribute("blogTypeList", blogTypeList);
		return "blogTypeList";
	}
	/**
	 * 跳转到添加博客类型页面
	 */
	@RequestMapping(value="/addBlogTypePage.do",method=RequestMethod.GET)
	public String addBlogTypePage(){
		
		return "addBlogTypePage";
	}
	/**
	 * 添加博客类型
	 */
	@RequestMapping(value="/addBlogType.do",method=RequestMethod.POST)
	public String addBlogType(BlogType blogType){
		blogTypeService.addBlogType(blogType);
		return "redirect:blogTypeList.do";
	}
	/**
	 * 删除博客类型
	 */
	@RequestMapping(value="/deleteBlogType.do",method=RequestMethod.GET)
	public String deleteBlogType(BlogType blogType){
		blogTypeService.deleteBlogType(blogType.getTypeId());
		return "redirect:blogTypeList.do";
	}
}
